home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / commands / rename.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  6.4 KB  |  216 lines

  1. /*
  2.  * rename.c --
  3.  * renameatt() and renamerel() reside here.
  4.  */
  5.  
  6. #include <strings.h>
  7.  
  8. #include "tmp/postgres.h"
  9.  
  10. RcsId("$Header: /private/postgres/src/commands/RCS/rename.c,v 1.8 1991/11/10 20:48:53 clarsen Exp $");
  11.  
  12. #include "nodes/pg_lisp.h"
  13.  
  14. #include "access/attnum.h"
  15. #include "access/heapam.h"
  16. #include "access/htup.h"
  17. #include "access/relscan.h"
  18. #include "access/skey.h"
  19. #include "access/tqual.h"
  20.  
  21. #include "catalog/catname.h"
  22. #include "catalog/syscache.h"
  23.  
  24. #include "commands/copy.h"
  25.  
  26. #include "executor/execdefs.h"    /* for EXEC_{FOR,BACK,FDEBUG,BDEBUG} */
  27.  
  28. #include "storage/buf.h"
  29. #include "storage/itemptr.h"
  30.  
  31. #include "tmp/miscadmin.h"
  32. #include "tmp/portal.h"
  33. #include "tcop/dest.h"
  34. #include "commands/command.h"
  35.  
  36. #include "utils/excid.h"
  37. #include "utils/log.h"
  38. #include "utils/mcxt.h"
  39. #include "utils/palloc.h"
  40. #include "utils/rel.h"
  41.  
  42. #include "catalog/pg_attribute.h"
  43. #include "catalog/pg_proc.h"
  44. #include "catalog/pg_relation.h"
  45. /*
  46.  *    rename        - See renameatt, renamerel
  47.  */
  48.  
  49. /*
  50.  *    renameatt    - changes the name of a attribute in a relation
  51.  *
  52.  *    Attname attribute is changed in attribute catalog.
  53.  *    No record of the previous attname is kept (correct?).
  54.  *
  55.  *    get proper reldesc from relation catalog (if not arg)
  56.  *    scan attribute catalog
  57.  *        for name conflict (within rel)
  58.  *        for original attribute (if not arg)
  59.  *    modify attname in attribute tuple
  60.  *    insert modified attribute in attribute catalog
  61.  *    delete original attribute from attribute catalog
  62.  *
  63.  *    XXX Renaming an indexed attribute must (eventually) also change
  64.  *        the attribute name in the associated indexes.
  65.  */
  66.  
  67. renameatt(relname, oldattname, newattname)
  68.     char    relname[], oldattname[], newattname[];
  69. {
  70.     Relation    relrdesc, attrdesc;
  71.     HeapScanDesc    relsdesc, attsdesc;
  72.     HeapTuple    reltup, oldatttup, newatttup;
  73.     struct skey    key[2];
  74.     ItemPointerData    oldTID;
  75.     int        issystem();
  76.     
  77.     if (issystem(relname)) {
  78.         elog(WARN, "renameatt: system relation \"%s\" not modified",
  79.              relname);
  80.         return;
  81.     }
  82.     ScanKeyEntryInitialize((ScanKeyEntry)&key[0], NULL, RelationNameAttributeNumber,
  83.                            Character16EqualRegProcedure, (Datum) relname);
  84.     relrdesc = heap_openr(RelationRelationName);
  85.     relsdesc = heap_beginscan(relrdesc, 0, NowTimeQual, 1, key);
  86.     reltup = heap_getnext(relsdesc, 0, (Buffer *) NULL);
  87.     if (!PointerIsValid(reltup)) {
  88.         heap_endscan(relsdesc);
  89.         heap_close(relrdesc);
  90.         elog(WARN, "renameatt: relation \"%s\" nonexistent",
  91.              relname);
  92.         return;
  93.     }
  94.     heap_endscan(relsdesc);
  95.     heap_close(relrdesc);
  96.  
  97.     ScanKeyEntryInitialize((ScanKeyEntry)&key[0], NULL, AttributeRelationIdAttributeNumber,
  98.                            Integer32EqualRegProcedure, (Datum)reltup->t_oid);
  99.     ScanKeyEntryInitialize((ScanKeyEntry)&key[1], NULL, AttributeNameAttributeNumber,
  100.                            Character16EqualRegProcedure, (Datum)oldattname);
  101.     attrdesc = heap_openr(AttributeRelationName);
  102.     attsdesc = heap_beginscan(attrdesc, 0, NowTimeQual, 2, key);
  103.     oldatttup = heap_getnext(attsdesc, 0, (Buffer *) NULL);
  104.     if (!PointerIsValid(oldatttup)) {
  105.         heap_endscan(attsdesc);
  106.         heap_close(attrdesc);    /* XXX should be unneeded eventually */
  107.         elog(WARN, "renameatt: attribute \"%s\" nonexistent",
  108.              oldattname);
  109.         return;
  110.     }
  111.     if (((struct attribute *) GETSTRUCT(oldatttup))->attnum < 0) {
  112.         elog(WARN, "renameatt: system attribute \"%s\" not renamed",
  113.              oldattname);
  114.         return;
  115.     }
  116.     oldatttup = palloctup(oldatttup, InvalidBuffer, attrdesc);
  117.     heap_endscan(attsdesc);
  118.  
  119.     key[1].sk_data = (DATUM) newattname;
  120.     attsdesc = heap_beginscan(attrdesc, 0, NowTimeQual, 2, key);
  121.     newatttup = heap_getnext(attsdesc, 0, (Buffer *) NULL);
  122.     if (PointerIsValid(newatttup)) {
  123.         pfree((char *) oldatttup);
  124.         heap_endscan(attsdesc);
  125.         heap_close(attrdesc);    /* XXX should be unneeded eventually */
  126.         elog(WARN, "renameatt: attribute \"%s\" exists",
  127.              newattname);
  128.         return;
  129.     }
  130.     heap_endscan(attsdesc);
  131.  
  132.     bcopy(newattname,
  133.           (char *) (((struct attribute *) GETSTRUCT(oldatttup))->attname),
  134.           sizeof(NameData));
  135.     oldTID = oldatttup->t_ctid;
  136.     heap_replace(attrdesc, &oldTID, oldatttup); /* insert "fixed" tuple */
  137.     heap_close(attrdesc);
  138.     pfree((char *) oldatttup);
  139. }
  140.  
  141. /*
  142.  *    renamerel    - change the name of a relation
  143.  *
  144.  *    Relname attribute is changed in relation catalog.
  145.  *    No record of the previous relname is kept (correct?).
  146.  *
  147.  *    scan relation catalog
  148.  *        for name conflict
  149.  *        for original relation (if not arg)
  150.  *    modify relname in relation tuple
  151.  *    insert modified relation in relation catalog
  152.  *    delete original relation from relation catalog
  153.  *
  154.  *    XXX Will currently lose track of a relation if it is unable to
  155.  *        properly replace the new relation tuple.
  156.  */
  157.  
  158. renamerel(oldrelname, newrelname)
  159.     char    oldrelname[], newrelname[];
  160. {
  161.     Relation    relrdesc;        /* for RELATION relation */
  162.     HeapScanDesc    oldsdesc, newsdesc;
  163.     HeapTuple    oldreltup, newreltup;
  164.     struct skey    key;
  165.     ItemPointerData    oldTID;
  166.     char        oldpath[MAXPGPATH], newpath[MAXPGPATH];
  167.     int        issystem();
  168.     char        *relpath();
  169.     extern        rename();
  170.     
  171.     if (issystem(oldrelname)) {
  172.         elog(WARN, "renamerel: system relation \"%s\" not renamed",
  173.              oldrelname);
  174.         return;
  175.     }
  176.     relrdesc = heap_openr(RelationRelationName);
  177.  
  178.     ScanKeyEntryInitialize((ScanKeyEntry)&key, NULL, RelationNameAttributeNumber,
  179.                            Character16EqualRegProcedure, (Datum) oldrelname);
  180.  
  181.     oldsdesc = heap_beginscan(relrdesc, 0, NowTimeQual, 1, &key);
  182.     oldreltup = heap_getnext(oldsdesc, 0, (Buffer *) NULL);
  183.     if (!PointerIsValid(oldreltup)) {
  184.         heap_endscan(oldsdesc);
  185.         heap_close(relrdesc);    /* XXX should be unneeded eventually */
  186.         elog(WARN, "renamerel: relation \"%s\" does not exist",
  187.              oldrelname);
  188.     }
  189.     oldreltup = palloctup(oldreltup, InvalidBuffer, relrdesc);
  190.  
  191.     key.sk_data = (DATUM) newrelname;
  192.     newsdesc = heap_beginscan(relrdesc, 0, NowTimeQual, 1, &key);
  193.     newreltup = heap_getnext(newsdesc, 0, (Buffer *) NULL);
  194.     if (PointerIsValid(newreltup)) {
  195.         pfree((char *) oldreltup);
  196.         heap_endscan(newsdesc);
  197.         heap_endscan(oldsdesc);
  198.         heap_close(relrdesc);    /* XXX should be unneeded eventually */
  199.         elog(WARN, "renamerel: relation \"%s\" exists",
  200.              newrelname);
  201.     }
  202.     heap_endscan(newsdesc);
  203.     bcopy(newrelname,
  204.           (char *) (((struct relation *) GETSTRUCT(oldreltup))->relname),
  205.           sizeof(NameData));
  206.     oldTID = oldreltup->t_ctid;
  207.     heap_replace(relrdesc, &oldTID, oldreltup); /* insert "fixed" tuple */
  208.     pfree((char *) oldreltup);
  209.     heap_endscan(oldsdesc);
  210.     heap_close(relrdesc);
  211.     (void) strcpy(oldpath, relpath(oldrelname));
  212.     (void) strcpy(newpath, relpath(newrelname));
  213.     if (rename(oldpath, newpath) < 0)
  214.         elog(WARN, "renamerel: unable to rename file: %m");
  215. }
  216.